const mysql = require('../config/db')
const { DataTypes } = require('sequelize')
require('dotenv').config()
const Comments = mysql.define(
  'comments',
  {
    id: {
      type: DataTypes.BIGINT,
      primaryKey: true,
      autoIncrement: true,
    },
    postId: {
      type: DataTypes.BIGINT,
      allowNull: false,
      comment: '文章id',
    },
    nickname: {
      type: DataTypes.STRING,
      allowNull: false,
      defaultValue: '匿名用户',
      comment: '昵称',
    },
    content: {
      type: DataTypes.TEXT,
    },
    email: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    avatar: {
      type: DataTypes.STRING,
      allowNull: false,
      defaultValue: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      comment: '头像',
    },
    status: {
      type: DataTypes.STRING,
      allowNull: false,
      defaultValue: 'approved',
      comment: 'pending ：待审核 approved：审核通过 rejected：审核拒绝',
    },
    ip: {
      type: DataTypes.STRING,
      allowNull: false,
      comment: 'ip',
    },
    device: {
      type: DataTypes.STRING,
      allowNull: false,
      comment: '设备',
    },
    level: {
      type: DataTypes.TINYINT,
      defaultValue: 1,
      validate: {
        min: 1,
        max: 2,
      },
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      comment: '创建时间',
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      comment: '更新时间',
    },
    website: {
      type: DataTypes.STRING,
      allowNull: false,
      defaultValue: null,
      comment: '个人主页',
    },
  },
  {
    indexes: [{ fields: ['postId'] }, { fields: ['parentId'] }, { fields: ['status'] }],
    paranoid: true,
    deletedAt: process.env.DELETED_AT,
    tableName: 'comments',
    timestamps: true,
    updatedAt: 'updatedAt',
    createdAt: 'createdAt',
  },
)
module.exports = Comments
